package cn.tedu.em.complaint.controller;

import cn.tedu.em.common.pojo.complaint.dto.ComplaintAddNewDTO;
import cn.tedu.em.common.pojo.complaint.dto.ComplaintUpdateDTO;
import cn.tedu.em.common.pojo.complaint.vo.ComplaintListItemVO;
import cn.tedu.em.common.pojo.complaint.vo.ComplaintStandardVO;
import cn.tedu.em.common.restful.JsonResult;
import cn.tedu.em.complaint.service.IComplaintService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/complaints")
@Api(tags = "投诉管理模块")
public class ComplaintController {

    @Autowired
    private IComplaintService complaintService;

    // http://localhost:30002/complaints/add-new
    @ApiOperation("添加投诉")
    @ApiOperationSupport(order = 100)
    @PostMapping("/add-new")
    public JsonResult addNew(ComplaintAddNewDTO complaintAddNewDTO){
        log.info("开始处理【添加投诉】的请求，参数：{}", complaintAddNewDTO);
        complaintService.addNew(complaintAddNewDTO);
        String message = "提交投诉成功!";
        return JsonResult.ok(message);
    }

    // http://localhost:30002/complaints/9527/delete
    @ApiOperation("根据ID删除投诉")
    @ApiOperationSupport(order = 200)
    @PostMapping("/{id:[0-9]+}/delete")
    public JsonResult delete(@PathVariable Long id){
        log.debug("开始处理【根据ID删除投诉】的请求，参数：{}", id);
        complaintService.delete(id);
        return JsonResult.ok("删除成功!");
    }

    // http://localhost:9080/complaints/9527/dispose
    @PostMapping("/{id:[0-9]+}/dispose")
    @ApiOperation("已处理投诉")
    @ApiOperationSupport(order = 310)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "类别ID", required = true, dataType = "long")
    })
    public JsonResult<Void> SetDispose(@PathVariable @Range(min = 1, message = "请提交有效的ID值！") Long id) {
        log.debug("开始处理【投诉状态】的请求，参数：{}", id);
        complaintService.SetDispose(id);
        return JsonResult.ok();
    }

    // http://localhost:9080/complaints/9527/undispose
    @PostMapping("/{id:[0-9]+}/undispose")
    @ApiOperation("未处理投诉")
    @ApiOperationSupport(order = 311)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "类别ID", required = true, dataType = "long")
    })
    public JsonResult<Void> SetUndispose(@PathVariable @Range(min = 1, message = "请提交有效的ID值！") Long id) {
        log.debug("开始处理【投诉状态】的请求，参数：{}", id);
        complaintService.SetUndispose(id);
        return JsonResult.ok();
    }

    // http://localhost:30002/complaints/9527/update
    @ApiOperation("修改投诉详情")
    @ApiOperationSupport(order = 300)
    @PostMapping("/{id:[0-9]+}/update")
    public JsonResult update(@PathVariable Long id, ComplaintUpdateDTO complaintUpdateDTO){
        log.debug("开始处理【修改投诉详情】的请求，参数ID：{}, 新数据：{}", id, complaintUpdateDTO);
        complaintService.updateInfoById(id, complaintUpdateDTO);
        return JsonResult.ok("修改成功!");
    }

    // http://localhost:30002/complaints/9527
    @ApiOperation("根据ID查询投诉详情")
    @ApiOperationSupport(order = 400)
    @GetMapping("/{id:[0-9]+}")
    public JsonResult<ComplaintStandardVO> getStandardById(@PathVariable Long id){
        log.debug("开始处理【根据ID查询投诉详情】的请求，参数：{}", id);
        ComplaintStandardVO complaint = complaintService.getStandardById(id);
        return JsonResult.ok(complaint);
    }

    // http://localhost:30002/complaints
    @ApiOperation("查询投诉列表")
    @ApiOperationSupport(order = 420)
    @GetMapping("")
    public JsonResult<List<ComplaintListItemVO>> list(){
        log.debug("开始处理【查询投诉列表】的请求，无参数");
        List<ComplaintListItemVO> list = complaintService.list();
        return JsonResult.ok(list);
    }


}
